﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Hospital_TraPurchasePlanList : System.Web.UI.Page,HZ.MBSM.BLL.TraDes
{
    public string strConditions;
    public int projectid;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            projectid = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["projectid"]));
            if (!IsPostBack)
            {
                DrBuyStatus.Items.Add("全部");
                DrBuyStatus.Items.Add("未选采购药品");
                DrBuyStatus.Items.Add("已选采购药品未提交");
                DrBuyStatus.Items.Add("已提交给配送人");
            }
            if (Request.Form["butDel"] == "删除" && Request.Form["chk"] != null)
            {
                string BuyPlanIdDel = "";
                BuyPlanIdDel = SysFun.ToTrim(Request.Form["chk"]);
                Db db = new Db();
                
                if (BuyPlanIdDel != "")
                {
                    string[] BGID = BuyPlanIdDel.Length == 0 ? new string[] { "" } : BuyPlanIdDel.Substring(0).Split(new char[] { ',' });
                    string strSql = "";
                    for (int i = 0; i < BGID.Length; i++)
                    {
                       strSql += "@" + "delete  from PurchasePlan  where BuyPlanId="+BGID[i].ToString()+" and buyplanid not in (select  BuyPlanId from BuyPlanGoods where dealstatus<>0 and BuyPlanId="+BGID[i].ToString()+")";
                       strSql += "@" + "delete from BuyPlanGoods  where BuyPlanId=" + BGID[i].ToString() + " and buyplanid not in (select  BuyPlanId from BuyPlanGoods where dealstatus<>0 and BuyPlanId=" + BGID[i].ToString() + ")";
                    }
                    if (strSql.Length > 0)
                    {
                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "删除", "PurchasePlan，BuyPlanGoods", strSql, "../Hospital/TraPurchasePlanList.aspx");
                        db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
                    }
                }
                db.Close();
                
            }
            //if (Request.Form["butTJ"] == "提 交" && Request.Form["chk"] != null)
            //{
            //    string BuyPlanIdDel = "";
            //    BuyPlanIdDel = SysFun.ToTrim(Request.Form["chk"]);
            //    Db db = new Db();
            //    if (BuyPlanIdDel != "")
            //    {
            //        string[] BGID = BuyPlanIdDel.Length == 0 ? new string[] { "" } : BuyPlanIdDel.Substring(0).Split(new char[] { ',' });

            //        string strSql = "";
            //        for (int i = 0; i < BGID.Length; i++)
            //        {
            //            strSql += "@" + "update  PurchasePlan set BuyStatus='2'  where BuyPlanId=" + BGID[i].ToString() + " and BuyStatus='1'";
            //        }
            //        if (strSql.Length > 0)
            //        {
            //            db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
            //        }
            //    }                
            //    db.Close();
            //}
            showGridView();
        }
    }


    public void showGridView()
    {
       
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "PurchasePlan";                                               //要查询的表明
        dbPage.Key = "BuyPlanId";                                          //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "BuyPlanId,PlanName,StartTime,EndTime,BuildTime,case  when    BuyStatus='0' then '未选采购药品' when  BuyStatus='1'then '已选采购药品未提交' when    BuyStatus='2' then '已提交给配送人'   end BuyStatus";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 15;                                                    //没有显示记录条数   
        dbPage.OrderBy = "BuildTime,BuyPlanId";                  //排序字段
        dbPage.OrderByIsDesc = true;
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        DlAttorney.DataSource = dr;
        DlAttorney.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键
    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        string BuyStatus, _strTemp = "", StrStartTime = "", StrEndTime = "", _strPlanName;

        //采购单状态
        BuyStatus = DrBuyStatus.SelectedValue.ToString();
        switch (BuyStatus)
        {
            case "全部":
                break;
            case "未选采购药品":
                _strTemp = " BuyStatus=0 and";
                break;
            case "已选采购药品未提交":
                _strTemp = " BuyStatus=1 and";
                break;
            case "已提交给配送人":
                _strTemp = " BuyStatus=2 and";
                break;
        }
        //_AccountName_SHR = lstSHR.SelectedValue;
        //  _strTemp +=  " projectid=" + projectid + " and AccountId_YY=" + _Account.AccountId + " and";
        _strTemp += "  AccountId_YY=" + _Account.AccountId + " and";

        //采购单名称
        _strPlanName = txtPlanName.Text.Trim();
        if (_strPlanName != "")
        {
            _strTemp += " PlanName  like'%" + _strPlanName + "%' and";
        }

        //起始时间
        StrStartTime = txtStartTime.Text.ToString();
        if (StrStartTime != "")
        {
            _strTemp += " StartTime>='" + StrStartTime + "' and";
        }
        //结束时间
        StrEndTime = txtEndTime.Text.ToString();
        if (StrEndTime != "")
        {
            _strTemp += " EndTime <='" + StrEndTime + "' and";
        }

        if (_strTemp != "")
        {
            _strTemp = "  " + _strTemp.Substring(0, _strTemp.Length - 3);
        }
        return _strTemp;
    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.open('PurchasePlanAdd.aspx?projectid=" + projectid + "');</script>");
    }
    //protected void BtnDel_Click(object sender, EventArgs e)
    //{
    //    string BuyPlanIdDel = "";
    //    BuyPlanIdDel = SysFun.ToTrim(Request.Form["chk"]);
    //    Db db = new Db();
    //    //是否已提交


    //    if (BuyPlanIdDel != "")
    //    {
    //        string[] BGID = BuyPlanIdDel.Length == 0 ? new string[] { "" } : BuyPlanIdDel.Substring(0).Split(new char[] { ',' });

    //        string strSql = "";
    //        for (int i = 0; i < BGID.Length; i++)
    //        {

    //            strSql += "@" + "delete  from PurchasePlan  where BuyPlanId=" + BGID[i].ToString() + " and BuyStatus='0'";
    //        }
    //        if (strSql.Length > 0)
    //        {
    //            db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
    //        }
    //    }
    //    db.Close();
    //    showGridView();
    //}
    //protected void Button2_Click(object sender, EventArgs e)
    //{
    //    string BuyPlanIdDel = "";
    //    BuyPlanIdDel = SysFun.ToTrim(Request.Form["chk"]);
    //    Db db = new Db();
    //    //是否已提交
    //    if (BuyPlanIdDel != "")
    //    {
    //        string[] BGID = BuyPlanIdDel.Length == 0 ? new string[] { "" } : BuyPlanIdDel.Substring(0).Split(new char[] { ',' });

    //        string strSql = "";
    //        for (int i = 0; i < BGID.Length; i++)
    //        {
    //            strSql += "@" + "update  PurchasePlan set BuyStatus='2'  where BuyPlanId=" + BGID[i].ToString() + " and BuyStatus='1'";
    //        }
    //        if (strSql.Length > 0)
    //        {
    //            db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
    //        }
    //    }
    //    showGridView(); 
    //    db.Close();
    //}

    #region TraDes 成员

    public string Name
    {
        get { return "采购单列表"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "采购单管理"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
            return _AccountType;
        }
    }

    #endregion

}